wifi.setmode(wifi.STATION)
tmr.alarm(1, 1000, 1, function() 
 if (wifi.sta.getip() ~= nil)  then
  tmr.stop(1)
  print("Config done, IP is "..wifi.sta.getip())
  cli=net.createConnection(net.TCP,0)
  cli:on("receive",recv)
  cli:dns("time.nist.gov", function(conn, ip) print("time.nist.gov:"..ip) end)
  cli:connect(37,"time.nist.gov")
end

end)

function nstol(str)
  assert(str and #str == 4)
  local t = {str:byte(1,-1)}
  local n = 121212345678
  n=0
  for k = 1, #t do
  	print(t[k])
   n= n*256 + t[k]
 end
 return n
end

function gettime(ss)
	local s =ss%60
	ss=ss/60
	local m=ss%60
	ss=ss/60
	local h = ss%24
	ss=ss/24
	
	print("d"..ss.." "..h..":"..m..":"..s)

end

function recv(sck,c)
	print("receive data...")
	secondsSince1970=nstol(c)
	print("time:"..secondsSince1970)
	--print(os.date("%a, %d %b %Y %X GMT", secondsSince1970))
	--gettime(secondsSince1970)
end

wifi.startsmart(0, function() 
 print("smart config success!")
 end)

